package servlet;

import bean.Comment;
import dao.entity.CommentDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "commentServlet", value = "/ccquery")
public class CommentQueryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取数
        String ccid = request.getParameter("ccid");
        String page = request.getParameter("currentPage");
        int currentPage = 1;//默认的当前页码为1
        if(page!=null){
            currentPage= Integer.parseInt(page);
            request.setAttribute("currentPage",currentPage);
        }
        CommentDaoImpl cdi = new CommentDaoImpl();//创建实现类的对象
        List<Comment> list = null;
            //查询全部
            list = cdi.comment_find(currentPage);
            int n = cdi.comment_count();//记录数
            int pages = (int) Math.ceil(n/5.0);//计算页数,向上取整4.1-->5.0
            request.setAttribute("ccid",ccid);
            request.setAttribute("list",list);
            request.setAttribute("pages",pages);
            request.getRequestDispatcher("comment.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
